﻿using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using FluentValidation.Results;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.IServices;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using BestTrader.Cms.Models;
using BestTrader.Cms.UserCenter.Validation;

namespace BestTrader.Cms.Admin.Controllers
{
    [Authorize]
    public class ManagerController : Controller
    {
        public String UserId { get { return User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; } }
        public String RoleId { get { return User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; } }

        private readonly IManagerService _service;
        private readonly IManagerBankCardService _bankCardService;
        private readonly IManagerRoleService _roleService;
        private readonly IUCExchangeRateService _uCExchangeRateService;
        private readonly IUCExchangeRateManagerService _uCExchangeRateManagerService;

        public ManagerController(IManagerService service, IManagerBankCardService bankCardService, IManagerRoleService roleService, IUCExchangeRateService uCExchangeRateService, IUCExchangeRateManagerService uCExchangeRateManagerService)
        {
            _service = service;
            _bankCardService = bankCardService;
            _roleService = roleService;
            _uCExchangeRateService = uCExchangeRateService;
            _uCExchangeRateManagerService = uCExchangeRateManagerService;

        }

        public IActionResult Index() => View();
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string LoadData([FromQuery]ManagerRequestModel model)
        {
            model.State = "1";
            return JsonHelper.ObjectToJSON(_service.LoadData(model, UserId, RoleId));
        }
        /// <summary>
        /// 获取用户汇率设置
        /// </summary>
        /// <param name="ManagerId"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetInOutExchangeRate([FromQuery]String ManagerId)
        {
            var model = _uCExchangeRateManagerService.GetExchangeRateManagerByManagerId(ManagerId);
            return JsonHelper.ObjectToJSON(model);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inCoinId"></param>
        /// <returns></returns>
        [HttpPost, ValidateAntiForgeryToken]
        public string AddOrModifyExchangeRate([FromForm]UCExchangeRateManagerModel item)
        {
            var result = _uCExchangeRateManagerService.AddOrModifyExchangeRate(item, UserId);
            return JsonHelper.ObjectToJSON(result);
        }

        /// <summary>
        /// 修改银行卡
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ModifyBankCard()
        {
            return View();
        }


        [HttpGet, ValidateAntiForgeryToken]
        public string GetManagerBankCard()
        {
            return JsonHelper.ObjectToJSON(_bankCardService.GetManagerBankCardByManagerId(UserId));
        }

        [HttpPost, ValidateAntiForgeryToken]
        public string ModifyBankCard([FromForm]ManagerModifyBankCardModel item)
        {
            var result = new BaseResult();
            ManagerBanckCardValidation validation = new ManagerBanckCardValidation();
            ValidationResult results = validation.Validate(item);
            if (results.IsValid)
            {
                 result = _bankCardService.ModifyBankCard(item, UserId);
            }
            else {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }
            return JsonHelper.ObjectToJSON(result);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult SettingExchangeRate([FromQuery]String ManagerId)
        {
            var rateList = _uCExchangeRateService.GetUCExchangeRateList();
            TempData["ManagerId"] = ManagerId;
            return View(rateList.Data);
        }



    }
}